import ProTable from '@ant-design/pro-table';
import {useState} from 'react';
import {defaultPagination, paginationTool} from '@/utils/util';
import {useMount, useRequest} from 'ahooks';

import {findByPageBackAgentList} from '@/services/ant-design-pro/users';
import {Tag} from 'antd';
import {getByMiniSerachList} from '@/services/ant-design-pro/agent';
import {Link} from "@umijs/preset-dumi/lib/theme";

const columns = [
  {
    title: '编号',
    dataIndex: 'id',
    search: false,
    key: 'id',
  },
  {
    title: '授权编号',
    dataIndex: 'invite',
    key: 'invite',
  },
  {
    title: '头像',
    dataIndex: 'avatar',
    valueType: 'avatar',
    key: 'avatar',
    search: false,
  },
  {
    title: '姓名',
    dataIndex: 'name',
    key: 'name',
    render: (_: any, row: API.AgentInvitationRelationship, index: number, action: any) => {
      return (
        <Link target="_blank" to={'/users/tree?userId=' + row?.id}>
          {_}
        </Link>
      );
    },
  },
  {
    title: '代理品牌',
    dataIndex: 'test',
    key: 'test',
    search: false,
  },
  {
    title: '下级',
    dataIndex: 'agentCount',
    search: false,
    key: 'agentCount',
  },
  {
    title: '微信号',
    dataIndex: 'wechat',
    key: 'wechat',
  },
  {
    title: '手机号',
    dataIndex: 'phone',
    key: 'phone',
  },
  {
    title: '邀请人',
    dataIndex: 'parentName',
    key: 'parentName',
  },
  {
    title: '代理等级',
    dataIndex: 'agentName',
    search: false,
    key: 'agentName',
  },
  {
    title: '代理等级',
    dataIndex: 'agentId',
    key: 'agentId',
    valueEnum: await getByMiniSerachList ( 0 ),
    valueType: 'select',
    hideInTable: true,
  },
  {
    title: '时间',
    dataIndex: 'createDate',
    valueType: 'dateTime',
    hideInSearch: true,
    key: 'createDate',
  },
  {
    title: '状态',
    dataIndex: 'state',
    key: 'state',
    search: false,
    render: () => {
      return <Tag color="#108ee9">已通过</Tag>;
    },
  },
  {
    title: '操作',
    width: '164px',
    key: 'option',
    valueType: 'option',
    render: (_: any, row: API.AgentInvitationRelationship, index: number, action: any) => [
      <Link key="link" to={'/users/detailed?userId=' + row.id}>
        详细
      </Link>,
    ],
  },
];
//
const cloneTbale = () => {
  const [pagination, setPagination] = useState<API.Pagination> ( defaultPagination () );
  const {
    run: fetchByPageList,
    refresh,
    data: dataList,
    loading,
  } = useRequest ( findByPageBackAgentList, {
    debounceWait: 100,
    manual: true,
  } );

  useMount ( () => {
    fetchByPageList ();
  } );

  return (
    <ProTable
      search={false}
      dataSource={dataList?.data?.list}
      loading={loading}
    pagination={paginationTool ( pagination, dataList?.data?.total, (data1: API.Pagination) => {
        setPagination ( data1 );

        fetchByPageList ( {...data1} );
      } )}
 /*     pagination={{
        pageSize: 10,
        total:dataList?.data?.total,
        onChange: (page,pageSize) => fetchByPageList ( {page:page,pageSize:pageSize} )
      }}*/
      search={{
        defaultCollapsed: false,
        labelWidth: 'auto',
      }}
      onSubmit={(params) => {
        fetchByPageList ( params );
        console.log ( 'onSubmit', params );
      }}
      cardBordered={true}
      columns={columns}
    ></ProTable>
  );
};

export default cloneTbale;
